import 'package:appupgrade/app_market.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'appupgrade_platform_interface.dart';

/// An implementation of [AppupgradePlatform] that uses method channels.
class MethodChannelAppupgrade extends AppupgradePlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('appupgrade');

  @override
  Future installAppForWindows(String path) async {
    var map = {'path': path};
    return await methodChannel.invokeMethod('executeUpdater', map);
  }

  @override
  Future installAppForMac(String path) async {
    var map = {'path': path};
    return await methodChannel.invokeMethod('runDmg', map);
  }

    ///
  /// Android 安装app
  ///
  @override
  Future installAppForAndroid(String path) async {
    var map = {'path': path};
    return await methodChannel.invokeMethod('install', map);
  }

  ///
  /// 跳转到ios app store
  ///
  @override
  Future toAppStore(String id) async {
    var map = {'id': id};
    return await methodChannel.invokeMethod('toAppStore', map);
  }

  ///
  /// 获取android手机上安装的应用商店
  ///
  @override
  Future<List<String>> getInstallMarket({List<String>? marketPackageNames}) async {
    List<String> packageNameList = AppMarket.buildInPackageNameList;
    if (marketPackageNames != null && marketPackageNames.length > 0) {
      packageNameList.addAll(marketPackageNames);
    }
    var map = {'packages': packageNameList};
    var result = await methodChannel.invokeMethod('getInstallMarket', map);
    List<String> resultList = (result as List).map((f) {
      return '$f';
    }).toList();
    return resultList;
  }

  ///
  /// 跳转到应用商店
  ///
  @override
  Future toMarket({required AppMarketInfo appMarketInfo}) async {
    var map = {
      'marketPackageName':
      appMarketInfo != null ? appMarketInfo.packageName : '',
      'marketClassName': appMarketInfo != null ? appMarketInfo.className : ''
    };
    return await methodChannel.invokeMethod('toMarket', map);
  }
}
